package com.bysc.baseservice.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqConfig {


    // 队列名称
    public static final String QUEUE_STUDY_SCORE = "study_score_queue";
    public static final String QUEUE_POST_SCORE = "post_score_queue";
    // 队列
    public static final String QUEUE_INFORM_NOTICE = "queue_inform_notice";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String QUEUE_INFORM_DDL = "queue_inform_ddl";
    public static final String QUEUE_INFORM_ACTIVITY_INIT = "queue_activity_init";
    public static final String QUEUE_INFORM_ACTIVITY_END= "queue_activity_end";
    public static final String QUEUE_INFORM_ACTIVITY_QUEUE = "queue_activity_queue";
    public static final String QUEUE_INFORM_ACTIVITY_JOIN = "queue_activity_join";

    // 交换机
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
    public static final String EXCHANGE_TOPICS_DDL = "exchange_topics_ddl";
    public static final String EXCHANGE_TOPICS_ACTIVITY = "exchange_topics_activity";
    public static final String EXCHANGE_STUDY_SCORE = "study_score_exchange";
    public static final String EXCHANGE_POST_SCORE = "post_score_exchange";

    // 路由Key
    public static final String ROUTINGKEY_NOTICE = "inform.#.notice.#";
    public static final String ROUTINGKEY_SMS = "inform.#.sms.#";
    public static final String ROUTINGKEY_DDL = "ddl";
    public static final String ROUTINGKEY_ACTIVITY_INIT = "join.activity.init";
    public static final String ROUTINGKEY_ACTIVITY_END = "join.activity.end";
    public static final String ROUTINGKEY_ACTIVITY_QUEUE = "join.activity.queue";
    public static final String ROUTINGKEY_ACTIVITY_JOIN = "join.activity.join";
    public static final String ROUTING_STUDY_SCORE = "study_score_routing";
    public static final String ROUTING_POST_SCORE = "post_score_routing";

    // 声明交换机
    @Primary
    @Bean(EXCHANGE_TOPICS_DDL)
    public Exchange EXCHANGE_TOPICS_DDL() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_DDL).durable(true).build();
    }

    // 声明交换机
    @Bean(EXCHANGE_TOPICS_ACTIVITY)
    @Primary
    public Exchange EXCHANGE_TOPICS_ACTIVITY() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_ACTIVITY).durable(true).build();
    }

    // 声明交换机
    @Primary
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    @Primary
    @Bean(QUEUE_INFORM_NOTICE)
    public Queue QUEUE_INFORM_NOTICE() {
        return new Queue(QUEUE_INFORM_NOTICE, true, false, false);
    }
    // 声明死信队列
    @Primary
    @Bean(QUEUE_INFORM_DDL)
    public Queue QUEUE_INFORM_DDL() {
        return new Queue(QUEUE_INFORM_DDL, true);
    }
    @Primary
    @Bean(QUEUE_INFORM_ACTIVITY_INIT)
    public Queue QUEUE_INFORM_ACTIVITY_INIT() {
        return new Queue(QUEUE_INFORM_ACTIVITY_INIT, true);
    }
    @Primary
    @Bean(QUEUE_INFORM_ACTIVITY_END)
    public Queue QUEUE_INFORM_ACTIVITY_END() {
        return new Queue(QUEUE_INFORM_ACTIVITY_END, true);
    }
    @Primary
    @Bean(QUEUE_INFORM_ACTIVITY_QUEUE)
    public Queue QUEUE_INFORM_ACTIVITY_QUEUE() {
        return new Queue(QUEUE_INFORM_ACTIVITY_QUEUE, true);
    }

    // 声明QUEUE_INFORM_SMS队列
    @Primary
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS() {
        return new Queue(QUEUE_INFORM_SMS, true, false, false);
    }

    // 声明QUEUE_INFORM_ACTIVITY_JOIN队列
    @Primary
    @Bean(QUEUE_INFORM_ACTIVITY_JOIN)
    public Queue QUEUE_INFORM_ACTIVITY_JOIN() {
        Map<String, Object> args = new HashMap<>();
        // 设置死信交换机和路由键
        args.put("x-dead-letter-exchange", EXCHANGE_TOPICS_DDL);
        args.put("x-dead-letter-routing-key", ROUTINGKEY_DDL);
        return new Queue(QUEUE_INFORM_ACTIVITY_JOIN, true, false, false, args);
    }

    @Primary
    @Bean
    public Binding BINDING_ACTIVITY_JOIN(@Qualifier(QUEUE_INFORM_ACTIVITY_JOIN) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_ACTIVITY) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_ACTIVITY_JOIN).noargs();
    }

    @Primary
    @Bean
    public Binding BINDING_ACTIVITY_INIT(@Qualifier(QUEUE_INFORM_ACTIVITY_INIT) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_ACTIVITY) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_ACTIVITY_INIT).noargs();
    }
    @Primary
    @Bean
    public Binding BINDING_ACTIVITY_END(@Qualifier(QUEUE_INFORM_ACTIVITY_END) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_ACTIVITY) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_ACTIVITY_END).noargs();
    }
    @Primary
    @Bean
    public Binding BINDING_ACTIVITY_QUEUE(@Qualifier(QUEUE_INFORM_ACTIVITY_QUEUE) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_ACTIVITY) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_ACTIVITY_QUEUE).noargs();
    }

    // 绑定死信队列到死信交换机
    @Primary
    @Bean
    public Binding BINDING_QUEUE_INFORM_DEAD_LETTER(
            @Qualifier(QUEUE_INFORM_DDL) Queue deadLetterQueue,
            @Qualifier(EXCHANGE_TOPICS_DDL) Exchange deadLetterExchange) {
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with(ROUTINGKEY_DDL).noargs();
    }


    // ROYTINGKEY_NOTICE队列绑定交换机
    @Primary
    @Bean
    public Binding BINDING_QUEUE_INFORM_NOTICE(@Qualifier(QUEUE_INFORM_NOTICE) Queue queue,
                                               @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_NOTICE).noargs();
    }

    // ROYTINGKEY_SMS队列绑定交换机
    @Primary
    @Bean
    public Binding BINDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                          @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }    // study_score 交换机
    @Primary
    @Bean(EXCHANGE_STUDY_SCORE)
    public Exchange studyScoreExchange() {
        return ExchangeBuilder.directExchange(EXCHANGE_STUDY_SCORE).durable(true).build();
    }

    // post_score 交换机
    @Primary
    @Bean(EXCHANGE_POST_SCORE)
    public Exchange postScoreExchange() {
        return ExchangeBuilder.directExchange(EXCHANGE_POST_SCORE).durable(true).build();
    }

    // study_score 队列
    @Primary
    @Bean(QUEUE_STUDY_SCORE)
    public Queue studyScoreQueue() {
        return new Queue(QUEUE_STUDY_SCORE, true);
    }

    // post_score 队列
    @Primary
    @Bean(QUEUE_POST_SCORE)
    public Queue postScoreQueue() {
        return new Queue(QUEUE_POST_SCORE, true);
    }

    // study_score 队列绑定到交换机
    @Primary
    @Bean
    public Binding bindingStudyScore(@Qualifier(QUEUE_STUDY_SCORE) Queue queue,
                                     @Qualifier(EXCHANGE_STUDY_SCORE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_STUDY_SCORE).noargs();
    }

    // post_score 队列绑定到交换机
    @Primary
    @Bean
    public Binding bindingPostScore(@Qualifier(QUEUE_POST_SCORE) Queue queue,
                                    @Qualifier(EXCHANGE_POST_SCORE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_POST_SCORE).noargs();
    }
}
